package fr.paris.lutece.plugins.customisation.dnd;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import fr.paris.lutece.plugins.customisation.business.tab.CustomisationTab;

/**
 * Provide drag and drop functionnalities to the pages 
 * 
 * @author ENSEIRB Team LutecePP
 */
public class DragAndDropCodeBuilder {

	private static final String DND_ID_PREFIX = "dnd_";

	private static final String LI_TAG_CLOSE = "</li>\n";
	
	private static final String LI_TAG_OPEN_BEGIN = "<li id=";
	
	private static final String LI_TAG_OPEN_END = " class=\"element\">\n";

	private List<String> dndIdList;

	public DragAndDropCodeBuilder() {
		this.dndIdList = new ArrayList<String>();
	}

	/**
	 * Surround the content string of a portlet with <li> tags, in order
	 * to give drag and drop abilities to the portlet
	 * 
	 * @return the new content of the portlet
	 * @param portletId the id of the portlet
	 * @param strPortletContent the content of the portlet 
	 * */
	public String surroundWithLiTag(int portletId,
			String strPortletContent) {

		// build the <li> tag identifier
		String liId = DND_ID_PREFIX + portletId;

		// add it to the identifier list
		dndIdList.add(liId);

		// build the <li> open tag
		String liIdTagBegin = LI_TAG_OPEN_BEGIN + liId + LI_TAG_OPEN_END;

		//surround content with the <li> tag
		return liIdTagBegin + strPortletContent + LI_TAG_CLOSE;
	}

	/**
	 * Grow the template model to give drag and drop abilities to the portlets 
	 * and to set the three columns sizes
	 * 
	 * @param rootModel the genuine template model
	 * @param tab the current tab
	 */

	public void buildDndTemplateModel(Map<String,Object> rootModel, CustomisationTab tab) {
		// write the ids in the javascript dnd enabling instructions
		rootModel.put("dnd_id_list", dndIdList);
		
		//the id of the tab is written in the page, in order to send XHR requests
		//which contains this id
		rootModel.put("tab_id", tab.getIdTab());
		
		//write the three sizes of the columns
		rootModel.put("w1", tab.getFirstColumnWidth() );
		rootModel.put("w2", tab.getSecondColumnWidth() );
		rootModel.put("w3", tab.getThirdColumnWidth());
	}


}
